package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CommentMapper;
import com.itheima.pojo.Comment;
import com.itheima.pojo.CommentVO;
import com.itheima.service.CommentService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 分页+条件查询
     * @param commentVO
     * @return
     */
    @Override
    public PageInfo<Comment> getList(CommentVO commentVO) {
        Integer currentPage = commentVO.getCurrentPage();
        Integer pageSize = commentVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(commentVO);
        List<Comment> comments=new ArrayList<>();
        if ("all".equals(commentVO.getSource())){
            comments = commentMapper.selectAll();
        }else {
            comments = commentMapper.selectByExample(example);
        }

       return new PageInfo<>(comments);

    }

    /**
     * 删除
     * @param commentVO
     */
    @Override
    public void delete(CommentVO commentVO) {
        Example example = createExample(commentVO);
        commentMapper.deleteByExample(example);
    }

    /**
     * 删除选中
     * @param commentVO
     */
    @Override
    public void deleteBatch(CommentVO commentVO) {
        Example example = createExample(commentVO);
        commentMapper.deleteByExample(example);
    }

    /**
     * 添加
     * @param commentVO
     */
    @Override
    public void add(CommentVO commentVO) {
        Comment comment = JSON.parseObject(JSON.toJSONString(commentVO), Comment.class);
        //生成id
        IdWorker idWorker=new IdWorker();
        String id = idWorker.nextId()+"";
        comment.setUid(id);
        commentMapper.insertSelective(comment);
    }

    /**
     * 编辑
     * @param commentVO
     */
    @Override
    public void edit(CommentVO commentVO) {
        Comment comment = JSON.parseObject(JSON.toJSONString(commentVO), Comment.class);
        //根据uid查询需要编辑的对象
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(commentVO!=null) {
            if (commentVO.getUid() != null && !"".equals(commentVO.getUid())) {
                criteria.andEqualTo("uid", commentVO.getUid());
            }
        }
        commentMapper.updateByExample(comment,example);
    }

    /**
     * 获取用户点赞信息
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> getPraiseListByUser(Integer currentPage, Integer pageSize) {
        //获取数据
        List<Comment> comments = commentMapper.getPraiseListByUser();
        PageHelper.startPage(currentPage,pageSize);
       return new PageInfo<>(comments);
    }

    private Example createExample(CommentVO commentVO) {
        Example example=new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(commentVO!=null) {
            // 用户名
            if (commentVO.getUsername() != null && !"".equals(commentVO.getUsername())) {
                criteria.andEqualTo("userName", commentVO.getUsername());
            }
            // 评论内容
            if (commentVO.getContent() != null && !"".equals(commentVO.getContent())) {
                criteria.andEqualTo("content", commentVO.getContent());
            }
            // 评论来源
            if (commentVO.getSource() != null && !"".equals(commentVO.getSource())) {
                criteria.andEqualTo("source", commentVO.getSource());
            }
            // 播客uid
            if (commentVO.getBlogUid() != null && !"".equals(commentVO.getBlogUid())) {
                criteria.andEqualTo("blogUid", commentVO.getBlogUid());
            }
            // 状态
            if (commentVO.getStatus() != null && !"".equals(commentVO.getStatus())) {
                criteria.andEqualTo("status", commentVO.getStatus());
            }
            // 回复某条评论的uid
            if (commentVO.getToUid() != null && !"".equals(commentVO.getToUid())) {
                criteria.andEqualTo("toUid", commentVO.getToUid());
            }
            // 回复某个人的uid
            if (commentVO.getToUserUid() != null && !"".equals(commentVO.getToUserUid())) {
                criteria.andEqualTo("toUserUid", commentVO.getToUserUid());
            }
            // 评论名
            if (commentVO.getType() != null && !"".equals(commentVO.getType())) {
                criteria.andEqualTo("type", commentVO.getType());
            }
            // 评论类型
            if (commentVO.getUid() != null && !"".equals(commentVO.getUid())) {
                criteria.andEqualTo("uid", commentVO.getUid());
            }
            // 用户uid
            if (commentVO.getUserUid() != null && !"".equals(commentVO.getUserUid())) {
                criteria.andEqualTo("userUid", commentVO.getUserUid());
            }
        }
        return example;
    }

}
